package com.xxx.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.lease.model.entity.*;
import com.xxx.lease.model.enums.ItemType;
import com.xxx.lease.web.admin.mapper.*;
import com.xxx.lease.web.admin.service.*;
import com.xxx.lease.web.admin.vo.attr.AttrValueVo;
import com.xxx.lease.web.admin.vo.graph.GraphVo;
import com.xxx.lease.web.admin.vo.room.RoomDetailVo;
import com.xxx.lease.web.admin.vo.room.RoomItemVo;
import com.xxx.lease.web.admin.vo.room.RoomQueryVo;
import com.xxx.lease.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* @author zlz
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2024-09-19 20:00:29
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService{

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;


    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(roomItemVoPage, queryVo);
    }

    @Override
    public void saveOrUpdateInfo(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        super.saveOrUpdate(roomSubmitVo);
        if (id != null){
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));

        }

        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();

        if(!CollectionUtils.isEmpty(graphVoList)){

            List<GraphInfo> graphInfoList = new ArrayList<>();

            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(id);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfoList.add(graphInfo);
            }

            graphInfoService.saveBatch(graphInfoList);

        }

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();

        if (!CollectionUtils.isEmpty(labelInfoIds)) {

            List<RoomLabel> roomLabelList = new ArrayList<>();

            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().build();
                roomLabel.setRoomId(roomSubmitVo.getId());
                roomLabel.setLabelId(labelInfoId);
                roomLabelList.add(roomLabel);
            }

            roomLabelService.saveBatch(roomLabelList);
        }

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();

        if (!CollectionUtils.isEmpty(facilityInfoIds)) {

            List<RoomFacility> roomFacilityList = new ArrayList<>();

            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().build();
                roomFacility.setRoomId(roomSubmitVo.getId());
                roomFacility.setFacilityId(facilityInfoId);
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }

        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();

        if (!CollectionUtils.isEmpty(attrValueIds)){

            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();

            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().build();
                roomAttrValue.setRoomId(roomSubmitVo.getId());
                roomAttrValue.setAttrValueId(attrValueId);
                roomAttrValueList.add(roomAttrValue);
            }

            roomAttrValueService.saveBatch(roomAttrValueList);

        }


        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();

        if (!CollectionUtils.isEmpty(leaseTermIds)){

            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();

            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().build();
                roomLeaseTerm.setRoomId(roomSubmitVo.getId());
                roomLeaseTerm.setLeaseTermId(leaseTermId);
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);

        }

        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();

        if (!CollectionUtils.isEmpty(paymentTypeIds)){

            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();

            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().build();
                roomPaymentType.setRoomId(roomSubmitVo.getId());
                roomPaymentType.setPaymentTypeId(paymentTypeId);
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }




    }

    @Override
    public RoomDetailVo getDetailById(Long id) {

        RoomDetailVo roomDetailVo = new RoomDetailVo();

        RoomInfo roomInfo = roomInfoMapper.selectById(id);

        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        List<GraphVo> graphVoList = graphInfoMapper.selectListByIdAndType(roomInfo.getId(), ItemType.ROOM);

        List<AttrValueVo> attrValueVoList =attrValueMapper.selectListByRoomId(roomInfo.getId());

        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(roomInfo.getId());

        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(roomInfo.getId());

        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(roomInfo.getId());

        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(roomInfo.getId());

        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);


        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {

        super.removeById(id);

        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));

        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));

        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));

        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));

        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));

        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));






    }
}




